var $ = require('../../lib/jquery');
var util = require('./common/util');
var minHeight = require('./common/minHeight');
minHeight.init();
var falg=true,countdown=180;
function settime() {
    if (countdown == 0) {
        falg=true;
        $('.js_code').html('获取验证码');
        $('.js_code').removeClass('js_code1');
        countdown = 180;
    } else {
        countdown--;
        $('.js_code').html(countdown+'s');
        $('.js_code').addClass('js_code1');
     
        setTimeout(function() {
            settime()
        },1000)
    }
}
$('.js_code').on('click',function(){
    var login=$('.login').val();
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(login)){
        alert('手机号有误') 
    }else{
        if(falg){
            falg=false;
            util.fetch('login/user/SendMessage?type=0&fun=6&phoneNum='+login).then(function(res){
                  settime();
                    sessionStorage.setItem('login',login);
            }).fail(function(res) {
                falg = true;
                 alert(res.msg)
            })
        }
        
    }
   
})
$('.js_login1').on('click',function(){
    var code=($('.code').val()|| '').replace(/s+/g,'');
   if(!code){
        alert('验证码不能为空') 
    }else{
        $('.js_message').removeClass('hide');
        util.fetch('login/user/validatCode?fun=6&login=' + sessionStorage.getItem('login') + '&code=' + code).then(function (res) {
            util.fetch('login/user/handleUserLogin?login=' + sessionStorage.getItem('login') + '&code=' + code).then(function (res) {
                sessionStorage.setItem('token', res.data.token);
                $('.js_message').addClass('hide')
                window.location.href = './index.html'

            }).fail(function (res) {
                alert(res.msg);
               
            })
        }).fail(function(res){
             $('.js_message').addClass('hide')
             alert(res.msg);
             falg = true;
             $('.js_code').html('获取验证码');
             $('.js_code').removeClass('js_code1');
             countdown = 0;
        })
       
    }

})

$('.js_login2').on('click',function(){
    var passWord=$('.js_word').val();
    var login=($('.js_phone').val() || '').replace(/s+/g,'');
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(login)){
        alert('手机号有误') 
    }
   else if (!login) {
        alert('不能为空') 
    }else{
        $('.js_message').removeClass('hide')
        util.fetch('login/user/handleUserLogin?type=0&login='+login+'&passWord='+passWord).then(function (res){
            window.location.href='./index.html';
            sessionStorage.setItem('login',login);
            sessionStorage.setItem('token',res.data.token);
            $('.js_message').addClass('hide')
            
        }).fail(function(res){
            alert(res.msg)
            $('.js_message').addClass('hide');
        })
    }
})
$('.js_second').on('click',function(){
    $('.first').removeClass('hide');
    $('.second').addClass('hide');
})
$('.js_first').on('click',function(){
    $('.first').addClass('hide');
    $('.second').removeClass('hide');
})
$('.js_registe').on('click',function(){
    window.location.href='./registe.html'
})
$('.js_find').on('click',function(){
    window.location.href='./find.html'
})
$('.js_a2').on('click', function () {
    location.href = 'http://223.71.187.80:9010/bmxt';
})